package com.gk.controller;

import com.alibaba.fastjson.JSONObject;
import com.gk.entity.Product;
import com.gk.feignclient.ProductClient;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Slf4j
@RestController
public class CategoryController {

    @Autowired
    private ProductClient productClient;

    @GetMapping("/list")
    public String list() {
        log.info("list ok!");
        return "list ok";
    }

    @RequestMapping("/category")
    public String category(){
        log.info("category service ....");

        //远程调用服务接口
//        String product = productClient.product();

       // String result = productClient.test("gm", 23);

        //String result = productClient.test2(new Product(1, "cdq", 28.22, new Date()));

        //String result = productClient.test3(new String[]{"21", "22", "23"});

        //String result = productClient.test4(new String[]{"21", "22", "23"});

        //Product product = productClient.product(21);

        String result = productClient.findByCategoryIdAndPage(1, 5, 1);
        System.out.println(result);

        //自定义json反序列化，对象转换为json；序列化：将字符串转换为对象
        JSONObject jsonObject = JSONObject.parseObject(result);

        System.out.println(jsonObject.get("total"));
        Object rows = jsonObject.get("rows");
        System.out.println(rows);

        //二次反序列化
        List<Product> products = jsonObject.parseArray(rows.toString(), Product.class);
        products.forEach(product -> {
            log.info("product:{}", product);
        });
        return result;
    }
}
